package com.lx.idgenerator.autoconfigure;

import adaper.RedisLeafInfoService;
import com.baomidou.mybatisplus.autoconfigure.MybatisPlusPropertiesCustomizer;
import com.lx.generator.aspect.TpsMonitoringAspect;
import com.lx.generator.monitor.TpsCounter;
import com.lx.generator.monitor.TpsMonitor;
import com.lx.idgenerator.adaper.MysqlLeafInfoService;
import com.lx.idgenerator.config.MpPropertiesCustomizer;
import com.lx.idgenerator.service.ILeafNodeService;
import com.lx.idgenerator.service.impl.LeafNodeServiceImpl;
import com.lx.idgenerator.segement.CacheSegmentIDGenerator;
import com.lx.idgenerator.segement.bus.FillSegmentUpdater;
import com.lx.idgenerator.segement.leaf.ILeafInfoService;
import com.lx.idgenerator.segement.policy.IFetchPolicy;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
import org.springframework.data.redis.connection.RedisClusterConfiguration;
import org.springframework.data.redis.connection.RedisPassword;
import org.springframework.data.redis.connection.RedisStandaloneConfiguration;
import org.springframework.data.redis.connection.jedis.JedisClientConfiguration;
import org.springframework.data.redis.connection.jedis.JedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.util.StringUtils;
import redis.client.RedisClient;
import redis.client.RedisClientManager;
import redis.client.SegmentRedisClient;
import redis.clients.jedis.JedisPoolConfig;

import java.time.Duration;

/**
 * @author wcy
 */
@Configuration
@ConditionalOnProperty(prefix = "id.generator",name = "type",havingValue = "segment")
@EnableConfigurationProperties({SegmentFetchProperties.class,SegmentRedisProperties.class})
@MapperScan(value = "com.lx.idgenerator.mapper")
@Import({MysqlConfiguration.class,RedisConfiguration.class})
public class SegmentCacheGenServiceAutoConfiguration {

    private com.lx.idgenerator.autoconfigure.RedisProperties masterRedisProperties;
    private com.lx.idgenerator.autoconfigure.RedisProperties backRedisProperties;

    /**
     *
     * @return
     */
    @Bean
    public FillSegmentUpdater fillSegmentUpdater(){
        return new FillSegmentUpdater();
    }




    /**
     * 基于Redis存储 注入相关Bean
     * @return
     */
    @Bean
    @ConditionalOnMissingBean(IFetchPolicy.class)
    public IFetchPolicy fetchPolicy(SegmentFetchProperties segmentFetchProperties){
        return new DefaultFetchPolicy(segmentFetchProperties);
    }

    /**
     * 分段缓存生成器
     * @param leafInfoService
     * @param fillSegmentUpdater
     * @param fetchPolicy
     * @return
     */
    @Bean
    public CacheSegmentIDGenerator cacheSegmentIDGenerator(ILeafInfoService leafInfoService,FillSegmentUpdater fillSegmentUpdater,IFetchPolicy fetchPolicy){
        return new CacheSegmentIDGenerator(leafInfoService,fillSegmentUpdater,fetchPolicy);
    }


    @Bean
    @ConditionalOnProperty(prefix = "id.generator.segment.fetch",name = "enableMonitor",havingValue = "true")
    public TpsCounter tpsCounter(){
        return new TpsCounter();
    }

    @Bean
    @ConditionalOnProperty(prefix = "id.generator.segment.fetch",name = "enableMonitor",havingValue = "true")
    public TpsMonitoringAspect tpsMonitoringAspect(TpsCounter tpsCounter){
        return new TpsMonitoringAspect(tpsCounter);
    }

    @Bean
    @ConditionalOnProperty(prefix = "id.generator.segment.fetch",name = "enableMonitor",havingValue = "true")
    public TpsMonitor tpsMonitor(TpsCounter tpsCounter,IFetchPolicy fetchPolicy){
        return new TpsMonitor(tpsCounter,fetchPolicy);
    }








}
